﻿using System;
using System.Data;

namespace System
{
    public sealed class SmartDataReader
    {
        //        public   static DateTime DefaultDate = Convert.ToDateTime("01/01/1753 12:00:00");
        public IDataReader Reader
        {
            get { return this.reader; }
        }

        public SmartDataReader(IDataReader reader)
        {
            this.reader = reader;
        }

        public int? GetInt32(String column)
        {
            int? data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (int?)reader[column];
            return data;
        }

        public long? GetInt64(String column)
        {
            long? data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (long?)reader[column];
            return data;
        }

        public short? GetInt16(String column)
        {
            short? data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (short?)reader[column];
            return data;
        }

        public float? GetFloat(String column)
        {
            float? data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (float?)reader[column];
            return data;
        }

        public double? GetDouble(String column)
        {
            double? data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (double?)reader[column];
            return data;
        }

        public decimal? GetDecimal(String column)
        {
            decimal? data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (decimal?)reader[column];
            return data;
        }

        public Single? GetSingle(String column)
        {
            Single? data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (Single?)reader[column];
            return data;
        }

        public bool? GetBoolean(String column)
        {
            bool? data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (bool?)reader[column];
            return data;
        }
        //此处返回null || ""?
        public String GetString(String column)
        {
            String data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : reader[column].ToString();
            return data;
        }

        public byte[] GetBytes(String column)
        {
            byte[] data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (byte[])reader[column];
            return data;
        }

        public byte GetByte(String column)
        {
            byte data = (reader.IsDBNull(reader.GetOrdinal(column))) ? (byte)0 : (byte)reader[column];
            return data;
        }

        public Guid GetGuid(String column)
        {
            String data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : reader[column].ToString();
            Guid guid = Guid.Empty;
            if (data != null)
            {
                guid = new Guid(data);
            }
            return guid;
        }

        public DateTime? GetDateTime(String column)
        {
            DateTime? data = (reader.IsDBNull(reader.GetOrdinal(column))) ? null : (DateTime?)reader[column];
            return data;
        }

        public bool Read()
        {
            return this.reader.Read();
        }
        private IDataReader reader;
    }

    public sealed class SmartDataRow
    {
        public DataRow Reader
        {
            get { return this.reader; }
        }

        public SmartDataRow(DataRow reader)
        {
            this.reader = reader;
        }

        public int? GetInt32(String column)
        {

            int? data = reader.IsNull(column) ? null : (int?)reader[column];
            return data;
        }

        public short? GetInt16(String column)
        {
            short? data = reader.IsNull(column) ? null : (short?)reader[column];
            return data;
        }

        public float? GetFloat(String column)
        {
            float? data = reader.IsNull(column) ? null : (float?)reader[column];
            return data;
        }

        public double? GetDouble(String column)
        {
            double? data = reader.IsNull(column) ? null : (double?)reader[column];
            return data;
        }

        public decimal? GetDecimal(String column)
        {
            decimal? data = reader.IsNull(column) ? null : (decimal?)reader[column];
            return data;
        }

        public Single? GetSingle(String column)
        {
            Single? data = reader.IsNull(column) ? null : (Single?)reader[column];
            return data;
        }

        public bool? GetBoolean(String column)
        {
            bool? data = reader.IsNull(column) ? null : (bool?)reader[column];
            return data;
        }
        //此处返回null || ""?
        public String GetString(String column)
        {
            String data = reader.IsNull(column) ? null : reader[column].ToString();
            return data;
        }

        public byte[] GetBytes(String column)
        {
            byte[] data = (reader.IsNull(column)) ? null : (byte[])reader[column];
            return data;
        }

        public byte GetByte(String column)
        {
            byte data = (reader.IsNull(column)) ? (byte)0 : (byte)reader[column];
            return data;
        }

        public Guid GetGuid(String column)
        {
            String data = reader.IsNull(column) ? null : reader[column].ToString();
            Guid guid = Guid.Empty;
            if (data != null)
            {
                guid = new Guid(data);
            }
            return guid;
        }

        public DateTime? GetDateTime(String column)
        {
            DateTime? data = reader.IsNull(column) ? null : (DateTime?)reader[column];
            return data;
        }
        private DataRow reader;
    }

    public sealed class SmartDataRowView
    {
        public DataRowView Reader
        {
            get { return this.reader; }
        }

        public SmartDataRowView(DataRowView reader)
        {
            this.reader = reader;
        }

        public int? GetInt32(String column)
        {

            int? data = reader.Row.IsNull(column) ? null : (int?)reader[column];
            return data;
        }

        public short? GetInt16(String column)
        {
            short? data = reader.Row.IsNull(column) ? null : (short?)reader[column];
            return data;
        }

        public float? GetFloat(String column)
        {
            float? data = reader.Row.IsNull(column) ? null : (float?)reader[column];
            return data;
        }

        public double? GetDouble(String column)
        {
            double? data = reader.Row.IsNull(column) ? null : (double?)reader[column];
            return data;
        }

        public decimal? GetDecimal(String column)
        {
            decimal? data = reader.Row.IsNull(column) ? null : (decimal?)reader[column];
            return data;
        }

        public Single? GetSingle(String column)
        {
            Single? data = reader.Row.IsNull(column) ? null : (Single?)reader[column];
            return data;
        }

        public bool? GetBoolean(String column)
        {
            bool? data = reader.Row.IsNull(column) ? null : (bool?)reader[column];
            return data;
        }
        //此处返回null || ""?
        public String GetString(String column)
        {
            String data = reader.Row.IsNull(column) ? null : reader[column].ToString();
            return data;
        }

        public byte[] GetBytes(String column)
        {
            byte[] data = (reader.Row.IsNull(column)) ? null : (byte[])reader[column];
            return data;
        }

        public byte GetByte(String column)
        {
            byte data = (reader.Row.IsNull(column)) ? (byte)0 : (byte)reader[column];
            return data;
        }

        public Guid GetGuid(String column)
        {
            String data = reader.Row.IsNull(column) ? null : reader[column].ToString();
            Guid guid = Guid.Empty;
            if (data != null)
            {
                guid = new Guid(data);
            }
            return guid;
        }

        public DateTime? GetDateTime(String column)
        {
            DateTime? data = reader.Row.IsNull(column) ? null : (DateTime?)reader[column];
            return data;
        }
        private DataRowView reader;
    }

    public static class SmartReader
    {
        public static object GetSmartValue(SmartDataReader smartrd, string fldName)
        {
            IDataReader dr = smartrd.Reader;
            Type type = dr.GetFieldType(dr.GetOrdinal(fldName));
            object objValue = null;
            switch (type.Name)
            {
                case "Int32": objValue = smartrd.GetInt32(fldName); break;
                case "Int16": objValue = smartrd.GetInt16(fldName); break;
                case "Int64": objValue = smartrd.GetInt64(fldName); break;
                case "Float": objValue = smartrd.GetFloat(fldName); break;
                case "Double": objValue = smartrd.GetDouble(fldName); break;
                case "Decimal": objValue = smartrd.GetDecimal(fldName); break;
                case "Single": objValue = smartrd.GetSingle(fldName); break;
                case "Boolean": objValue = smartrd.GetBoolean(fldName); break;
                case "String": objValue = smartrd.GetString(fldName); break;
                case "Byte[]": objValue = smartrd.GetBytes(fldName); break;
                case "Byte": objValue = smartrd.GetByte(fldName); break;
                case "Guid": objValue = smartrd.GetGuid(fldName); break;
                case "DateTime": objValue = smartrd.GetDateTime(fldName); break;
                default: break;
            }
            return objValue;
        }

        public static object GetSmartValue(SmartDataRow sdr, string fldName)
        {
            DataRow dr = sdr.Reader;

            Type type = dr.Table.Columns[fldName].DataType;
            object value = null;

            switch (type.Name)
            {
                case "Int32": value = sdr.GetInt32(fldName); break;
                case "Int16": value = sdr.GetInt16(fldName); break;
                case "Float": value = sdr.GetFloat(fldName); break;
                case "Double": value = sdr.GetDouble(fldName); break;
                case "Decimal": value = sdr.GetDecimal(fldName); break;
                case "Single": value = sdr.GetSingle(fldName); break;
                case "Boolean": value = sdr.GetBoolean(fldName); break;
                case "String": value = sdr.GetString(fldName); break;
                case "Byte[]": value = sdr.GetBytes(fldName); break;
                case "Byte": value = sdr.GetByte(fldName); break;
                case "Guid": value = sdr.GetGuid(fldName); break;
                case "DateTime": value = sdr.GetDateTime(fldName); break;
                default: break;
            }
            return value;
        }

        public static object GetSmartValue(SmartDataRowView sdr, string fldName)
        {
            DataRowView drv = sdr.Reader;
            Type type = drv.DataView.Table.Columns[fldName].DataType;
            object objValue = null;

            switch (type.Name)
            {
                case "Int32": objValue = sdr.GetInt32(fldName); break;
                case "Int16": objValue = sdr.GetInt16(fldName); break;
                case "Float": objValue = sdr.GetFloat(fldName); break;
                case "Double": objValue = sdr.GetDouble(fldName); break;
                case "Decimal": objValue = sdr.GetDecimal(fldName); break;
                case "Single": objValue = sdr.GetSingle(fldName); break;
                case "Boolean": objValue = sdr.GetBoolean(fldName); break;
                case "String": objValue = sdr.GetString(fldName); break;
                case "Byte[]": objValue = sdr.GetBytes(fldName); break;
                case "Byte": objValue = sdr.GetByte(fldName); break;
                case "Guid": objValue = sdr.GetGuid(fldName); break;
                case "DateTime": objValue = sdr.GetDateTime(fldName); break;
                default: break;
            }
            return objValue;
        }
    }
}
